package com.weiaibenpao.demo.chislim.adater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.MyGameBean;
import com.weiaibenpao.demo.chislim.ui.MatchDetailActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/12.
 */

public class MatchListAdapter extends RecyclerView.Adapter {
    private List<MyGameBean.DataBean.ListBean> mData ;
    private Context mContext;

    public MatchListAdapter(Context context) {
        this.mContext = context;
        this.mData = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_unofficial_match,parent,false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyGameBean.DataBean.ListBean data = mData.get(position);
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.setData(data);
    }

    public void addData(List<MyGameBean.DataBean.ListBean> dataList){
        if (dataList != null) {
            mData.addAll(dataList);
        }
    }

    public void clear(){
        mData.clear();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.tv_match_id)
        TextView tvMatchId;
        @BindView(R.id.tv_match_name)
        TextView tvMatchName;
        @BindView(R.id.tv_start_time)
        TextView tvMatchStartTime;
        @BindView(R.id.tv_time_limit)
        TextView tvMatchTimeLimit;
        @BindView(R.id.tv_enrollment)
        TextView tvMatchEnrollment;


        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(this);
        }


        public void  setData(MyGameBean.DataBean.ListBean data){
            if(data == null) return;
            tvMatchId.setText(data.getActId());//此id是match供检索和宣传用的的展示id，不是标识身份的id
            tvMatchName.setText(data.getMarathonTitle());
            tvMatchStartTime.setText("开始时间: "+data.getSportsStartTime());
            tvMatchTimeLimit.setText("比赛时长: "+data.getThroughTime());
            tvMatchEnrollment.setText("报名人数: "+data.getJoinNumber()+"/"+data.getNumber()+"人");
        }

        @Override
        public void onClick(View v) {
            MatchDetailActivity.openActivity(v.getContext(),mData.get(getAdapterPosition()).getId());
        }
    }
}
